feat: persist and restore target height via MetadataStorage#476
feat: persist and restore target height via MetadataStorage#476xdustinface merged 1 commit intov0.42-devfrom
MetadataStorage#476Conversation
📝 WalkthroughWalkthroughThe patch adds persistent tracking of the last target block height: new metadata storage API methods, DiskStorageManager implementations, BlockHeadersManager now accepts and stores a metadata storage handle, loads persisted target height on init, and writes updated target height during sync events. Changes
Sequence Diagram(s)sequenceDiagram
participant Lifecycle as Lifecycle
participant BHM as BlockHeadersManager
participant MS as MetadataStorage
participant CM as CheckpointManager
Lifecycle->>BHM: new(header_storage, metadata_storage, checkpoint_manager)
BHM->>MS: load_last_target_height()
alt persisted height found
MS-->>BHM: CoreBlockHeight
BHM->>BHM: set progress target = persisted height
else not found / error
MS-->>BHM: NotFound / Error
BHM->>BHM: set progress target = tip height
end
BHM-->>Lifecycle: initialized
Note over BHM,MS: during sync updates
Lifecycle->>BHM: on_network_event(best_height)
BHM->>MS: store_last_target_height(new_target)
MS-->>BHM: Result
BHM-->>Lifecycle: continue sync
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
89ab17e to
8e08c35
Compare
|
This PR has merge conflicts with the base branch. Please rebase or merge the base branch into your branch to resolve them. |
8e08c35 to
640c1f8
Compare
9db640f to
3bbea97
Compare
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (1)
dash-spv/src/sync/block_headers/sync_manager.rs (1)
155-156: Consider potential I/O on every peer update.The target height is persisted on every
PeersUpdatedevent with a newbest_height. While this ensures durability, it may result in frequent disk writes during peer discovery. This is acceptable for correctness since atomic writes are used, but consider adding a note or verifying that the write frequency is acceptable in high-churn scenarios.🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@dash-spv/src/sync/block_headers/sync_manager.rs` around lines 155 - 156, The code persists target height on every PeersUpdated event which may cause excessive I/O; modify the logic around metadata_storage.store_last_target_height to avoid writing on every update — for example, read the current persisted height (via metadata_storage) and only call store_last_target_height when *best_height* is greater than the stored value or when a configured debounce/interval has elapsed; locate this in sync_manager.rs where PeersUpdated is handled and update the branch that references metadata_storage.store_last_target_height to perform the check or debounce before persisting.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@dash-spv/src/storage/metadata.rs`:
- Around line 19-22: The doc comment for load_last_target_height is incorrect —
update the documentation above the async fn load_last_target_height(&self) ->
StorageResult<CoreBlockHeight> to say "Load the last target height from metadata
storage and restore progress" (or similar wording matching
store_last_target_height) so the comment accurately describes
load_last_target_height's purpose; ensure the doc text mirrors the
store_last_target_height description style for consistency.
---
Nitpick comments:
In `@dash-spv/src/sync/block_headers/sync_manager.rs`:
- Around line 155-156: The code persists target height on every PeersUpdated
event which may cause excessive I/O; modify the logic around
metadata_storage.store_last_target_height to avoid writing on every update — for
example, read the current persisted height (via metadata_storage) and only call
store_last_target_height when *best_height* is greater than the stored value or
when a configured debounce/interval has elapsed; locate this in sync_manager.rs
where PeersUpdated is handled and update the branch that references
metadata_storage.store_last_target_height to perform the check or debounce
before persisting.
ℹ️ Review info
Configuration used: defaults
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (6)
dash-spv/src/client/lifecycle.rsdash-spv/src/storage/metadata.rsdash-spv/src/storage/mod.rsdash-spv/src/sync/block_headers/manager.rsdash-spv/src/sync/block_headers/sync_manager.rsdash-spv/src/sync/sync_coordinator.rs
Introduce new functions in `MetadataStorage` to save/load the last known target height, and integrate it into the block headers manager. This allows the UI to show a more realistic state after restart during sync before connections are established.
3bbea97 to
4c5a25d
Compare
There was a problem hiding this comment.
🧹 Nitpick comments (1)
dash-spv/src/storage/metadata.rs (1)
103-107: Consider preserving the original error instead of mapping toCorruption.The
Err(e)case wraps anyload_metadatafailure asStorageError::Corruption, but the underlying error could be an I/O error, permission issue, or other non-corruption failure. This loses error context and may mislead debugging efforts.♻️ Suggested improvement
Err(e) => { - let error = format!("Failed to load last target height: {}", e); - tracing::warn!(error); - Err(StorageError::Corruption(error)) + tracing::warn!("Failed to load last target height: {}", e); + Err(e) }🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@dash-spv/src/storage/metadata.rs` around lines 103 - 107, The current Err(e) arm wraps any failure as StorageError::Corruption (see the Err(e) block that formats "Failed to load last target height") which loses the original error context; change it to preserve or propagate the original error (e.g., return the underlying error, wrap it in a more appropriate StorageError variant like Io or include e as the source) instead of unconditionally using StorageError::Corruption so callers of load_metadata / the function that loads the last target height can inspect the real cause.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@dash-spv/src/storage/metadata.rs`:
- Around line 103-107: The current Err(e) arm wraps any failure as
StorageError::Corruption (see the Err(e) block that formats "Failed to load last
target height") which loses the original error context; change it to preserve or
propagate the original error (e.g., return the underlying error, wrap it in a
more appropriate StorageError variant like Io or include e as the source)
instead of unconditionally using StorageError::Corruption so callers of
load_metadata / the function that loads the last target height can inspect the
real cause.
ℹ️ Review info
Configuration used: defaults
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (6)
dash-spv/src/client/lifecycle.rsdash-spv/src/storage/metadata.rsdash-spv/src/storage/mod.rsdash-spv/src/sync/block_headers/manager.rsdash-spv/src/sync/block_headers/sync_manager.rsdash-spv/src/sync/sync_coordinator.rs
🚧 Files skipped from review as they are similar to previous changes (1)
- dash-spv/src/sync/block_headers/sync_manager.rs
Introduce new functions in
MetadataStorageto save/load the last known target height, and integrate it into the block headers manager. This allows the UI to show a more realistic state after restart during sync before connections are established.Based on:
Summary by CodeRabbit